<?php


namespace addon\hotel\hotel\controller;


use addon\hotel\model\OrderCommon;
use addon\hotel\model\RoomType as RoomTypeModel;
use addon\hotel\model\Room as RoomModel;


class Room extends BaseHotel
{

    //房间列表
    public function lists()
    {

        if (request()->isAjax()){

            $room_model = new RoomModel();

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $search_text = input('search_text','');

            $type_id = input('type_id','');

            $alias = 'a';

            $join = [
                /*[
                    'hotel_room_type rt',
                    'rt.baoyu_type_id = a.type_id',
                    'left'
                ]*/
                /*[
                    'hotel_room_no rt',
                    'rt.type_id = a.type_id',
                    'left'
                ]*/
                [ 'hotel_room_type rt', 'a.type_id = rt.baoyu_type_id', 'inner' ]
            ];

            $condition = [
                ['a.hotel_id','=',$this->hotel_id],
                ['rt.hotel_id','=',$this->hotel_id]
            ];

            if ($search_text) {
                $condition[] = [ 'a.roomNo', 'like', '%' . $search_text . '%' ];
            }
            if ($type_id){
                $condition[] = [ 'a.type_id', '=', $type_id  ];
            }

            $order = 'a.sort desc,a.room_id desc';

            $field = 'a.*,rt.type_name';

            $list = $room_model->getRoomPageList($condition, $page, $page_size,$order,$field,$alias,$join);

            return $list;
        }

        $room_type_model = new RoomTypeModel();

        $condition = [
            ['hotel_id','=',$this->hotel_id]
        ];

        $type_list = $room_type_model->getRoomTypeList($condition,'type_id,hotel_id,type_name,baoyu_type_id');
        $this->assign('type_list',$type_list['data']);

        return $this->fetch('room/lists', [], $this->replace);
    }


    //添加房间
    public function addRoom()
    {
        $room_model = new RoomModel();

        if (request()->isAjax()){

            $data = [
                'room_name' => input('room_name', ''),
                'hotel_id' => $this->hotel_id,
                'stock' => input('stock', ''),
                'type_id' => input('type_id', ''),
                'price' => input('price', ''),
                'sort' => input('sort', 0),
                'label_format' => input('label_format', ''),
                'images' => input('images', 0),
                'detail' => input('detail', ''),
                'create_time' => time(),
                'baoyu_id'=>input('baoyu_id','')
            ];

            $res = $room_model->addRoom($data);
            return $res;

        }

        $room_type_model = new RoomTypeModel();

        $room_type = $room_type_model->getRoomTypeList([['hotel_id','=',$this->hotel_id]]);

        $this->assign('room_type_list',$room_type['data']);

        $hotel = model('hotel')->getInfo(['hotel_id'=>$this->hotel_id],'direct');

        $this->assign('direct',$hotel['direct']);

        return $this->fetch('room/add_room', [], $this->replace);
    }

    //编辑房间
    public function editRoom()
    {
        $room_id = input("room_id", 0);

        $room_model = new RoomModel();
        if (request()->isAjax()){

            $data = [
                'room_name' => input('room_name', ''),
                'stock' => input('stock', ''),
                'type_id' => input('type_id', ''),
                'price' => input('price', ''),
                'sort' => input('sort', 0),
                'label_format' => input('label_format', ''),
                'images' => input('images', 0),
                'detail' => input('detail', ''),
                'modify_time' => time(),
                'baoyu_id'=>input('baoyu_id','')
            ];

            $room_id = input("room_id", 0);

            $condition = array (
                [ "room_id", "=", $room_id ]
            );

            $res = $room_model->editRoom($data,$condition);
            return $res;

        }
        $info = $room_model->getRoomInfo($room_id);

        $room_no_list = [];
        if($info['data']['baoyu_id'] != 0 && $info['data']['baoyu_id'] != '') {
            $coupon = new \addon\hotel\model\RoomNo();
            $room_no_list = $coupon->getRoomNoList([ ['state','=','S'],['baoyu_id','in',$info['data']['baoyu_id']] ]);

            $room_no_list = $room_no_list['data'];

        }
        $info['data']['room_no_list'] = $room_no_list;

        $this->assign('info',$info['data']);

        $room_type_model = new RoomTypeModel();

        $room_type = $room_type_model->getRoomTypeList([['hotel_id','=',$this->hotel_id]]);

        $this->assign('room_type_list',$room_type['data']);

        $hotel = model('hotel')->getInfo(['hotel_id'=>$this->hotel_id],'direct');

        $this->assign('direct',$hotel['direct']);

        return $this->fetch('room/edit_room', [], $this->replace);
    }

    public function deleteRoom()
    {
        if (request()->isAjax()) {
            $room_ids = input('room_ids', 0);
            $room_model = new RoomModel();
            return $room_model->deleteRoom([ [ 'room_id', 'in', $room_ids ] ]);
        }
    }

    public function operationRoom()
    {
        if (request()->isAjax()) {
            $room_model = new RoomModel();

            $room_id = input('room_id', 0);

            $status = input('status', 0);

            if ($status==1){
                $room_info = $room_model->getRoomInfo($room_id);
                $room_info = $room_info['data'];
                if (empty($room_info['price'])||empty($room_info['stock'])||empty($room_info['images'])||empty($room_info['detail'])){
                    return $room_model->error('','请先完善房间信息');
                }
            }


            return $room_model->editRoom(['status'=>$status],[ [ 'room_id', '=', $room_id ] ]);
        }
    }

    public function editRoomField()
    {
        if (request()->isAjax()) {
            $adv = new RoomModel();
            $type = input('type', '');
            $value = input('value', '');
            $room_id = input('room_id', '');
            $data = [
                $type => $value
            ];
            return $adv->editRoom($data, [ [ 'room_id', '=', $room_id ] ]);
        }
    }
}